package org.example.spring_book.model;

import lombok.Data;
import org.example.spring_book.constant.Constants;
import org.yaml.snakeyaml.scanner.Constant;

@Data
public class Result<T> {
    private int code;  //200成功  -1用户未登录 -2程序出错  这是业务状态码  不是HTTP的状态码
    private String errMsg;
    private T data;

    public static <T> Result success(T data) {
        Result result = new Result();
        result.setCode(Constants.SUCCESS_CODE);
        result.setErrMsg("");
        result.setData(data);
        return result;
    }

    public static <T> Result unlogin() {
        Result result = new Result();
        result.setCode(Constants.UNLOGIN_CODE);
        result.setErrMsg("用户未登录");
        return result;
    }

    public static <T> Result fail(T data) {
        Result result = new Result();
        result.setCode(Constants.FAIL_CODE);
        result.setErrMsg("程序发生错误");
        return result;
    }

    public static <T> Result fail(String errMsg) {
        Result result = new Result();
        result.setCode(Constants.FAIL_CODE);
        result.setErrMsg(errMsg);
        return result;
    }

    public static <T> Result fail(String errMsg, int code) {
        Result result = new Result();
        result.setCode(code);
        result.setErrMsg(errMsg);
        return result;
    }

    public static <T> Result fail(){
        Result result = new Result();
        result.setCode(Constants.FAIL_CODE);
        result.setErrMsg("程序发生错误!");
        return result;
    }
}
